<template>
  <div class="login">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <h4>欢迎登陆</h4>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="small"
      >
        
        <el-form-item prop="username">
          <el-input v-model.number="ruleForm.username" placeholder="用户名">
              <el-button slot="prepend" icon="el-icon-user-solid"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="密码" autocomplete="off">
              <el-button slot="prepend" icon="el-icon-lock"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="w-100" type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      }else {
        callback()
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }else {
        callback()
      }
    };
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            const res = await this.login()
            const { token,username } = res.data
            this.$store.commit('setToken', token)
            this.$store.commit('setUsername', username)
            this.$router.push({
              name: 'index'
            })
          }catch(err) {
            this.$message.error(err)
          }
          
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    login() {
      return this.http('login', 'post', this.ruleForm).then(res => {
        this.$message({
          message: res.meta.msg,
          type: 'success'
        })
        return res
      }).catch(err=>{})
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style scoped>
.login {
  height: 100vh;
  width: 100vw;
  background-image: url(https://file.iviewui.com/admin-dist/img/login-bg.0899ffa6.jpg);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 300px;
  margin-right: 150px;
}
</style>